package vm.project;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import jxl.CellType;
import jxl.LabelCell;
import jxl.NumberCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class GestionStations {
    
    public HashMap<String,StationsInfos> listeStations = new HashMap<>();
    public FiltreNom filtre;
    
    public void lireFichierXLS(String fichier) throws IOException, BiffException{
        
            Workbook workbook = Workbook.getWorkbook(new File(fichier));      
            Sheet sheet = workbook.getSheet(0);
            int i;
            for(i=0;i<sheet.getRows();i++){
                if(sheet.getCell(0,i).getType() == CellType.NUMBER){
                    listeStations.put(((LabelCell)sheet.getCell(1,i)).getString()
                                                    , new StationsInfos(
                                                            ((LabelCell)sheet.getCell(1,i)).getString(),
                                                            ((NumberCell)sheet.getCell(5, i)).getValue(),
                                                            ((NumberCell)sheet.getCell(6, i)).getValue()));
                }
            }            
    }
    
    public double getXof(String name){
        return listeStations.get(this.filtre(name)).getX();
    }
    public double getYof(String name){
        return listeStations.get(this.filtre(name)).getY();
    }

    public double getXMin(){
        double courant = Double.MAX_VALUE;
        if(!listeStations.isEmpty()){
            for(StationsInfos p : listeStations.values()){
                    if(p.getX()<courant){
                        courant=p.getX();
                    }
            }
        }
        return courant;
    }

    public double getYMin(){
        double courant = Double.MAX_VALUE;
        if(!listeStations.isEmpty()){
            for(StationsInfos p : listeStations.values()){
                    if(p.getY()<courant){
                        courant=p.getY();
                    }
            }
        }
        return courant;
    }

    public double getXMax(){
        double courant = Double.MIN_VALUE;
        if(!listeStations.isEmpty()){
            for(StationsInfos p : listeStations.values()){
                    if(p.getX()>courant){
                        courant=p.getX();
                    }
            }
        }
        return courant;
    }

    public double getYMax(){
        double courant = Double.MIN_VALUE;
        if(!listeStations.isEmpty()){
            for(StationsInfos p : listeStations.values()){
                    if(p.getY()>courant){
                        courant=p.getY();
                    }
            }
        }
        return courant;
    }
    
    public void initFiltre(String fichier){ 
        filtre = new FiltreNom(fichier);
       // System.out.println(filtre);
    }
    
    public FiltreNom getFiltre(){
        return this.filtre;
    }
    
    public String filtre(String name){
        if(filtre!=null){
            return filtre.get(name);
        }
        return name;
    }
    
    
}
